package top.kaoshanji.ch04;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import top.kaoshanji.ch04.param.Employee;

/**
 * 代码清单 ch04-04：方法参数的Employee
 * author kaoshanji
 * ^_^
 * create time 2024/11/25 11:10
 */
public class ParamTest {

    private static final Logger logger = LogManager.getLogger(ParamTest.class);

    public static void main(String[] args) {
        logger.info("...代码清单 ch04-04：方法参数的Employee...");

        logger.info("...测试1：方法不能修改基本数据类型的参数（即数值型或布尔型）...");
        System.out.println("Testing tripleValue:");
        double percent = 10;
        System.out.println("Before: percent=" + percent);
        tripleValue(percent);
        System.out.println("After: percent=" + percent);


        logger.info("...测试2：方法可以修改对象参数的状态...");
        System.out.println("\nTesting tripleSalary:");
        var harry = new Employee("Harry", 50000);
        System.out.println("Before: salary=" + harry.getSalary());
        tripleSalary(harry);
        System.out.println("After: salary=" + harry.getSalary());


        logger.info("...测试3：方法不能让一个对象参数引用一个新的对象...");
        System.out.println("\nTesting swap:");
        var a = new Employee("Alice", 70000);
        var b = new Employee("Bob", 60000);
        System.out.println("Before: a=" + a.getName());
        System.out.println("Before: b=" + b.getName());
        swap(a, b);
        System.out.println("After: a=" + a.getName());
        System.out.println("After: b=" + b.getName());

    }

    // 不能工作
    public static void tripleValue(double x) {
        x = 3 * x;
        System.out.println("End of method: x=" + x);
    }

    // 可以工作
    public static void tripleSalary(Employee x) {
        x.raiseSalary(200);
        System.out.println("End of method: salary=" + x.getSalary());
    }

    public static void swap(Employee x, Employee y) {
        Employee temp = x;
        x = y;
        y = temp;

        System.out.println("End of method: x=" + x.getName());
        System.out.println("End of method: y=" + y.getName());
    }


}
